package com.lanou.musicproject.activity;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.lanou.musicproject.R;
import com.lanou.musicproject.SQLite.MyManagerDatabase;
import com.lanou.musicproject.base.BaseActivity;
import com.lanou.musicproject.utils.MyConfig;
import com.lanou.musicproject.bean.RankContent;
import com.lanou.musicproject.bean.RankShowLink;
import com.lanou.musicproject.volley.VolleySingleton;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by dllo on 15/10/21.
 * <p/>
 * 下载时插入数据库
 */
public class DownloadActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private String url, song_id;
    private VolleySingleton volleySingleton;
    private List<RankShowLink.SongurlEntity.UrlEntity> data;
    private RadioGroup rank_download_radiogroup;
    private RankContent.SongListEntity song_entity;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download_activity);
        volleySingleton = VolleySingleton.getInstance(this);
        rank_download_radiogroup = (RadioGroup) findViewById(R.id.rank_download_radiogroup);
        rank_download_radiogroup.setOnCheckedChangeListener(this);

        MyManagerDatabase helper = new MyManagerDatabase(DownloadActivity.this, "downloaded.db", null, 1);
        db = helper.getWritableDatabase();

        Intent intent = getIntent();
        if (intent != null) {
            song_entity = (RankContent.SongListEntity) intent.getSerializableExtra("entity");
            song_id = song_entity.getSong_id();
            Log.i("++++++++-----", song_id);
            url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.song.getInfos&format=json&songid=" +
                    song_id + "&ts=1445399459391&e=SmJtjLwgnuWRzBbIIHeXgNusY1L" +
                    "%2FXZoWyOitaQ3ExdPXWpSPZ0Fras1q%2FLVxivZr&nw=2&fr=sns&ucf=1&res=1&l2p=2943&lpb=&usup=1&lebo=0";
        }
        initData();
    }

    private void initData() {
        JsonObjectRequest jr = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String str = response.toString();
                RankShowLink link = JSON.parseObject(str, RankShowLink.class);
                data = link.getSongurl().getUrl();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(jr);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {

            case R.id.rank_download_standard:

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("下载选择");
                builder.setMessage("是否选择下载");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //此处需要传下载数据下载
                        if (data == null) {
                            Log.i("--------null------", "null");
                            Toast.makeText(DownloadActivity.this, "抱歉,暂无此资源", Toast.LENGTH_LONG).show();
                        } else {
                            String show_link = data.get(0).getShow_link();
                            MyConfig.putSharePre(DownloadActivity.this, "link", "url", show_link);
                            Toast.makeText(DownloadActivity.this, "你下载标准品质歌曲", Toast.LENGTH_LONG).show();
                            Log.i("++++++++--------->>", show_link);


                            //插入数据库
                            insertDatabase();
                            finish();

                            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                                Environment.getExternalStorageDirectory().getPath();
                                download(show_link, new File("/mnt/sdcard/Download/"));

                            }
                        }


                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DownloadActivity.this, "你取消了本次下载", Toast.LENGTH_LONG).show();
                    }
                });
                builder.create().show();

                break;
            case R.id.rank_download_hstandard:

                AlertDialog.Builder builderO = new AlertDialog.Builder(this);
                builderO.setTitle("下载选择");
                builderO.setMessage("是否选择下载");
                builderO.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //此处需要传下载数据下载
                        if (data == null) {
                            Log.i("--------null------", "null");

                            Toast.makeText(DownloadActivity.this, "抱歉,暂无此资源", Toast.LENGTH_LONG).show();
                        } else {
                            String show_link = data.get(1).getShow_link();
                            MyConfig.putSharePre(DownloadActivity.this, "link", "url", show_link);
                            Toast.makeText(DownloadActivity.this, "你下载高品质歌曲", Toast.LENGTH_LONG).show();
                            Log.i("++++++++--------->>", show_link);

                            if (Environment.getExternalStorageState().
                                    equals(Environment.MEDIA_MOUNTED)) {

                                Environment.getExternalStorageDirectory().getPath();
                                download(show_link, new File("/mnt/sdcard/Download/"));

                                insertDatabase();
                                finish();
                            }
                        }
                    }
                });
                builderO.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DownloadActivity.this, "你取消了本次下载", Toast.LENGTH_LONG).show();
                    }
                });
                builderO.create().show();
                break;
            case R.id.rank_download_xhstandard:
                AlertDialog.Builder builderS = new AlertDialog.Builder(this);
                builderS.setTitle("下载选择");
                builderS.setMessage("是否选择下载");
                builderS.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //此处需要传下载数据下载
                        if (data == null) {
                            Log.i("--------null------", "null");

                            Toast.makeText(DownloadActivity.this, "抱歉,暂无此资源", Toast.LENGTH_LONG).show();

                        } else {
                            String show_link = data.get(2).getShow_link();
                            MyConfig.putSharePre(DownloadActivity.this, "link", "url", show_link);
                            Toast.makeText(DownloadActivity.this, "你下载超高品质歌曲", Toast.LENGTH_LONG).show();
                            Log.i("++++++++--------->>", show_link);


                            if (Environment.getExternalStorageState().
                                    equals(Environment.MEDIA_MOUNTED)) {
                                Environment.getExternalStorageDirectory().getPath();
                                download(show_link, new File("/mnt/sdcard/Download/"));
                                insertDatabase();
                                finish();
                            }
                        }
                    }
                });
                builderS.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DownloadActivity.this, "你取消了本次下载", Toast.LENGTH_LONG).show();
                    }
                });
                builderS.create().show();
                break;
            case R.id.rank_download_xxhstandard:
                AlertDialog.Builder builderT = new AlertDialog.Builder(this);
                builderT.setTitle("下载选择");
                builderT.setMessage("是否选择下载");
                builderT.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //此处需要传下载数据下载

                        if (data == null) {
                            Log.i("--------null------", "null");
                            Toast.makeText(DownloadActivity.this, "抱歉,暂无此资源", Toast.LENGTH_LONG).show();
                        } else {

                            String show_link = data.get(3).getShow_link();
                            MyConfig.putSharePre(DownloadActivity.this, "link", "url", show_link);
                            Toast.makeText(DownloadActivity.this, "你下载无损品质歌曲", Toast.LENGTH_LONG).show();
                            Log.i("++++++++--------->>", show_link);


                            /**
                             *
                             * dir = /storage/emulated/0
                             *
                             * */
                            if (Environment.getExternalStorageState().
                                    equals(Environment.MEDIA_MOUNTED)) {
                                Environment.getExternalStorageDirectory().getPath();
                                download(show_link, new File("/mnt/sdcard/Download/"));

                                insertDatabase();


                                finish();
                            }
                        }
                    }
                });
                builderT.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DownloadActivity.this, "你取消了本次下载", Toast.LENGTH_LONG).show();
                    }
                });
                builderT.create().show();

                break;
        }


    }


    public void insertDatabase() {
        ContentValues values = new ContentValues();
        if (song_entity != null) {


            Log.i("---database---", "???????");

            values.put(MyManagerDatabase.column_song, song_entity.getTitle());
            values.put(MyManagerDatabase.column_singer, song_entity.getArtist_name());
            values.put(MyManagerDatabase.column_image, song_entity.getPic_small());


            Log.i("---database---", "!!!!!!!!!");

            db.insert(MyManagerDatabase.downloaded, null, values);


        } else {
            Log.i("---database---", "<<<>>>");
        }

    }
}
